﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RibbonEditor.Controls
{
    public partial class BooleanControl : UserControl, IDirtable
    {
        bool firstAccess = true;

        bool? initialState = null;

        public bool IsDirty
        {
            set { if (value == false) this.initialState = this.chkValue.Checked; this.isDirty = value; }
            get { return this.isDirty; }
        }

        private bool isDirty;

        public string Label
        {
            get
            {
                return this.lblLabel.Text;
            }
            set
            {
                this.lblLabel.Text = value;
            }
        }

        public bool Value
        {
            get
            {
                return this.chkValue.Checked;
            }
            set
            {
                if (!firstAccess)
                {
                    if (initialState == null)
                        initialState = value;
                }
                else
                {
                    firstAccess = false;
                }

                this.chkValue.Checked = value;
            }
        }

        public bool IsRequired
        {
            get
            {
                return this.lblRequired.Visible;
            }
            set
            {
                this.lblRequired.Visible = value;
            }
        }

        bool isEditable = true;

        public bool IsEditable
        {
            get { return isEditable; }
            set { isEditable = value; }
        }

        public BooleanControl()
        {
            //this.chkValue.CheckedChanged -= new System.EventHandler(this.chkValue_CheckedChanged);

            InitializeComponent();

            this.chkValue.Enabled = isEditable;

            this.chkValue.CheckedChanged += new System.EventHandler(this.chkValue_CheckedChanged);
        }

        private void chkValue_CheckedChanged(object sender, EventArgs e)
        {
            this.isDirty = this.chkValue.Checked != this.initialState;
        }

        public void ReinitValues()
        {
            this.chkValue.Checked = this.initialState.Value;
            this.isDirty = false;
        }
    }
}
